package servicesImp.business;

import base.IBaseMapperWithoutBLOBs;
import base.IBaseServicesWithoutBLOBsImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import services.business.BusinessInfoServices;
import services.business.InspectorFundServices;
import services.business.InspectorInfoServices;
import wt.dao.business.InspectorFundMapper;
import wt.entity.business.BusinessInfo;
import wt.entity.business.InspectorFund;
import wt.entity.business.InspectorFundExample;
import wt.entity.business.InspectorInfo;
import wt.staticData.ShortData;
import wt.staticData.TableName;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/10/14.
 */
@Service
public class InspectorFundServicesImp extends IBaseServicesWithoutBLOBsImpl< InspectorFund, InspectorFundExample> implements InspectorFundServices {
    @Autowired
    InspectorFundMapper inspectorFundMapper;
    @Autowired
    BusinessInfoServices businessInfoServices;
    @Autowired
    InspectorInfoServices inspectorInfoServices;

    @Override
    public IBaseMapperWithoutBLOBs<InspectorFund, InspectorFundExample> getMapper() {
        return this.inspectorFundMapper;
    }

    @Override
    public String getTableName() {
        return TableName.BUS_INSPECTOR_FUND;
    }


    @Transactional
    public void insertSelective(List<InspectorFund> inspectorFundList, String businessId, String operatorId, String operatorName) throws Exception {
        Date now = new Date();
        BusinessInfo businessInfo = new BusinessInfo();
        businessInfo.setId(businessId);
        businessInfo.setFundCreatePersonId(operatorId);
        businessInfo.setFundCreatePersonName(operatorName);
        businessInfo.setFundStatus(ShortData.s1);
        businessInfo.setFundCreateTime(now);
        businessInfoServices.updateByPrimaryKeySelective(businessInfo);
        for (InspectorFund inspectorFund : inspectorFundList) {
            inspectorFund.setCreateTime(now);
            inspectorFund.setStatus(0);
            inspectorFund.setBusinessId(businessId);
            getCurrentProxy().insertSelective(inspectorFund);
        }


    }

    @Transactional
    public void updateByPrimaryKeySelective(List<InspectorFund> inspectorFundList, String businessId, String operatorId, String operatorName) throws Exception {
        Date now = new Date();
        BusinessInfo businessInfo = new BusinessInfo();
        businessInfo.setId(businessId);
        businessInfo.setFundCheckPersonId(operatorId);
        businessInfo.setFundCheckPersonName(operatorName);
        businessInfo.setFundCheckTime(now);
        businessInfo.setFundStatus(ShortData.s2);
        businessInfoServices.updateByPrimaryKeySelective(businessInfo);

        for (InspectorFund inspectorFund : inspectorFundList) {
            inspectorFund.setCheckTime(now);
            inspectorFund.setStatus(1);
            getCurrentProxy().updateByPrimaryKeySelective(inspectorFund);
        }

    }

    public void lazyLoadInspectorInfo(List<InspectorFund> inspectorFundList) throws Exception {
        Map<String, InspectorInfo> map = new HashMap<String, InspectorInfo>();
        for (InspectorFund inspectorFund : inspectorFundList) {
            if (!map.containsKey(inspectorFund.getInspectorId())) {
                map.put(inspectorFund.getInspectorId(), inspectorInfoServices.selectByPrimaryKey(inspectorFund.getInspectorId()));
            }
            inspectorFund.setInspectorInfo(map.get(inspectorFund.getInspectorId()));
        }
    }

    public void lazyLoadBusinessInfo(List<InspectorFund> inspectorFundList) throws Exception {
        Map<String, BusinessInfo> map = new HashMap<String, BusinessInfo>();
        for (InspectorFund inspectorFund : inspectorFundList) {
            if (!map.containsKey(inspectorFund.getBusinessId())) {
                map.put(inspectorFund.getBusinessId(), businessInfoServices.selectByPrimaryKey(inspectorFund.getBusinessId()));
            }
            inspectorFund.setBusinessInfo(map.get(inspectorFund.getBusinessId()));
        }
    }

    public List<InspectorFund> selectByBusinessId(String businessId) throws Exception {
        InspectorFundExample inspectorFundExample = new InspectorFundExample();
        inspectorFundExample.createCriteria().andBusinessIdEqualTo(businessId);
        return getCurrentProxy().selectByExample(inspectorFundExample);
    }
}
